var dateIDs = $('#calendarStartDate, #calendarEndDate');

/***************************
	Flash Content
 ***************************/
if($('#flashContent1').length) {
	swfobject.embedSWF(
		$('#flashContent1').attr("root") + "files/ShowMeSmart.swf", 
		"flashContent1", 
		"250", 	
		"250", 
		"9.0.0", 
		$('#flashContent1').attr("root") + "files/expressInstall.swf",
		{},
		{ "wmode": "transparent", "salign": "bl" },
		{ "align": "left" }
	);
}

/***************************
	Accordions
 ***************************/
$('.accordion').collapse();

$('.accordion').on("show", function() {
		$(this).find('i').removeClass('icon-chevron-right').addClass('icon-chevron-down');
}).on("hide", function() {
	$(this).find('i').removeClass('icon-chevron-down').addClass('icon-chevron-right');
});

/***************************
	Validation Engine
 ***************************/
$(".validateForm").validationEngine('attach', {
	promptPosition : "centerRight",
	scroll: false
});

$('.validateFormButton').on("click", function() {
	var $form = $($(this).attr("form-id"));
	
	if($form.validationEngine("validate")) {
		$form.submit();
	}
});

/***************************
	Add Buttons
 ***************************/
$("body").on("click", ".addButton", function() {
	var $modal = $('div#modal');
	var action = $(this).attr("action");
	var title = $(this).attr("title");

	$modal.on("shown", function() {
		$('#updateForm').validationEngine("attach", { 'promptPosition': 'centerRight' })
		$('body').css('overflow', 'hidden');
	}).on('hide', function() {
		$('#updateForm').validationEngine("detach");
		$('body').css('overflow', 'auto');
	});

	$.post(action, function(source) {
		$modal.find('.modal-body p').html(source);
		$modal.find('.modal-header h3').html(title);
		$modal.modal();
	});
});

/***************************
	Edit Buttons
 ***************************/
$("body").on("click", ".editButton", function() {
	var $form = $('#updateForm');
	var $modal = $('div#modal');
	var action = $(this).attr("action");
	var title = $(this).attr("title");

	$modal.on("shown", function() {
		$('#updateForm').validationEngine("attach", { 'promptPosition': 'centerRight' })
		$('body').css('overflow', 'hidden');
	}).on('hide', function() {
		$('#updateForm').validationEngine("detach");
		$('body').css('overflow', 'auto');
	});

	$.post(action, function(source) {
		$modal.find('.modal-body p').html(source);
		$modal.find('.modal-header h3').html(title);
		$modal.modal();
	});
});

/***************************
	Save Buttons
 ***************************/
$("body").on("click", ".saveButton", function() {
	if($('#updateForm').validationEngine("validate")) {
		var $itemList = $('#itemList');
		var itemList = ($('#items').val() == null) ? 0 : $('#items').val();
			$itemList.val(itemList);
			
		$('#updateForm').submit();
	}
});

/***************************
	Calendar Buttons
 ***************************/

$('body').on("click", ".calendarAddButton", function() {
	var $modal = $('div#modal');

	$modal.on("shown", function() {
		$('#calendarEventForm').validationEngine("attach", { 'promptPosition': 'centerRight' })
			.find('input').val();
		$('body').css('overflow', 'hidden');
	}).on('hide', function() {
		$('#updateForm').validationEngine("detach");
		$('body').css('overflow', 'auto');
	});

	$modal.modal();
});

if(dateIDs.length > 0) {
	dateIDs.datepicker();
}

/***************************
	Tooltips
 ***************************/
$('.addButton, .deleteButton, .backButton').tipsy({
	gravity: 'w',
	fade: true,
	live: true
});

$('.editButton').tipsy({
	gravity: 'e',
	fade: true,
	live: true
});

$('.calendarAddButton').tipsy({
	gravity: 'sw',
	fade: true,
	live: true
});

/***************************
	Numeric Input Fields
 ***************************/
$('body').delegate('.numeric', 'keydown', function(event) {
	var isLetter = event.keyCode > 65 && event.keyCode < 90;
	var isChar = event.keyCode > 188 && event.keyCode < 222;

	 if($.browser.msie || $.browser.webkit)
		isChar = isChar && event.keyCode > 186 && event.keyCode < 187 && event.keyCode == 189;
	else if($.browser.mozilla)
		isChar = isChar && event.keyCode == 109 && event.keyCode == 107 && event.keyCode == 59;
	else if($.browser.opera)
		isChar = isChar && event.keyCode == 59 & event.keyCode == 61 && event.keyCode == 109;

	if(isLetter || isChar)
		return false;
	else return true;
}).on("keyup", '.numeric', function(event) {
	if($(this).hasClass('phone-3'))
		var maxLength = 3;
	else if($(this).hasClass('phone-4'))
		var maxLength = 4;

	if($(this).val().length == maxLength)
		$(this).nextAll('.numeric:first').focus().select();	
});

destroyLessCache('/ShowMeSmart/LESS/showMesmart.less');

function validateEmail() {
	alert($('input[name=email]').val());
}

// Destroys the localStorage copy of CSS that less.js creates
function destroyLessCache(pathToCss) { // e.g. '/css/' or '/stylesheets/'  
	if (!window.localStorage || !less || less.env !== 'development') {
		return;
	}

	var host = window.location.host;  var protocol = window.location.protocol;
	var keyPrefix = protocol + '//' + host + pathToCss;

	for (var key in window.localStorage) {
		if (key.indexOf(keyPrefix) === 0) {
			delete window.localStorage[key];
		}
	}
}
